/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package poo.biblioteca.vistas.editoriales;

import biblioteca.entidades.Editorial;
import biblioteca.entidades.Pais;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import javax.swing.JOptionPane;
import poo.biblioteca.vistas.IfrBase;

/**
 *
 * @author Alex
 */
public class IfrMnttoEditorial extends IfrBase {

    /**
     * Creates new form IfrMnttoEditorial
     */
    public IfrMnttoEditorial() {
        initComponents();
    }

    public String getNombreEditorial() {
        return txtNombreEditorial.getText();
    }
    
    public Editorial getEditorialSeleccionada() {
        int filaSeleccionada = tblEditoriales.getSelectedRow();
        if (filaSeleccionada < 0) {
            return null;
        }
        Editorial editorialSeleccionada = ((TbmEditorial) tblEditoriales.getModel())
                .getEditorialEnFila(filaSeleccionada);

        return editorialSeleccionada;
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txtNombreEditorial = new javax.swing.JTextField();
        lblNombreEditorial = new javax.swing.JLabel();
        btnBuscar = new javax.swing.JButton();
        scrMnttoEditorial = new javax.swing.JScrollPane();
        tblEditoriales = new javax.swing.JTable();
        btnModificar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setClosable(true);
        setTitle("Mantenimiento de Editoriales");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Búsqueda"));

        lblNombreEditorial.setText("Ingrese nombre:");

        btnBuscar.setMnemonic('B');
        btnBuscar.setText("Buscar");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblNombreEditorial)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtNombreEditorial)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnBuscar)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNombreEditorial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNombreEditorial)
                    .addComponent(btnBuscar))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        scrMnttoEditorial.setBorder(javax.swing.BorderFactory.createTitledBorder("Resultados"));

        tblEditoriales.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        scrMnttoEditorial.setViewportView(tblEditoriales);

        btnModificar.setMnemonic('M');
        btnModificar.setText("Modificar");
        btnModificar.setEnabled(false);

        btnEliminar.setMnemonic('E');
        btnEliminar.setText("Eliminar");
        btnEliminar.setEnabled(false);

        btnCancelar.setMnemonic('C');
        btnCancelar.setText("Cancelar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrMnttoEditorial, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 465, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnEliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnModificar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrMnttoEditorial, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnModificar)
                    .addComponent(btnEliminar)
                    .addComponent(btnCancelar))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblNombreEditorial;
    private javax.swing.JScrollPane scrMnttoEditorial;
    private javax.swing.JTable tblEditoriales;
    private javax.swing.JTextField txtNombreEditorial;
    // End of variables declaration//GEN-END:variables

    public void btnBuscarAddActionListener(ActionListener actionListener) {
        btnBuscar.addActionListener(actionListener);
    }

    public void setTableModel(TbmEditorial tbmEditorial) {
        tblEditoriales.setModel(tbmEditorial);
    }

    public void permitirEdicion(boolean permitir) {
        btnModificar.setEnabled(permitir);
        btnEliminar.setEnabled(permitir);
    }

    public void btnEliminarAddActionListener(ActionListener actionListener) {
        btnEliminar.addActionListener(actionListener);
    }

    public boolean confirmarEliminacion() {
        return (JOptionPane.showConfirmDialog(this, "¿Está seguro de eliminar "
                + "la Editorial seleccionada?", "Confirmación", 
                JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION ? 
                true:false);
    }

    public void deseleccionarTabla() {
        tblEditoriales.clearSelection();
        permitirEdicion(false);
    }

    public void eliminarEditorialSeleccionada() {
        
        getTableModel().eliminar(tblEditoriales.getSelectedRow());
    }

    public TbmEditorial getTableModel() {
        return (TbmEditorial) tblEditoriales.getModel();
    }

    public int getFilaSeleccionada() {
        return tblEditoriales.getSelectedRow();
    }

    public void btnCancelarAddActionListener(ActionListener actionListener) {
        btnCancelar.addActionListener(actionListener);
    }

    public void tblEditorialesAddMouseListener(MouseAdapter mouseAdapter) {
        tblEditoriales.addMouseListener(mouseAdapter);
    }

    public boolean confirmarModificacion() {
        return (JOptionPane.showConfirmDialog(this, "¿Está seguro de modificar "
                + "la Editorial seleccionada?", "Confirmación", 
                JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION ? 
                true:false);
    }

    public void btnModificarAddActionListener(ActionListener actionListener) {
        btnModificar.addActionListener(actionListener);
    }

    public void modificarEditorialSeleccionada(Editorial nuevaEditorial) {
        getTableModel().modificar(tblEditoriales.getSelectedRow(), nuevaEditorial);
    }
    

}
